package com.shizhanzhe.gxb.my.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.shizhanzhe.gxb.R;
import com.shizhanzhe.gxb.base.UserBean;
import com.shizhanzhe.gxb.my.adapter.MyOrderAdapter;
import com.shizhanzhe.gxb.my.bean.MyOrder;
import com.shizhanzhe.gxb.my.bean.TeamOrder;
import com.shizhanzhe.gxb.url.Path;
import com.shizhanzhe.gxb.utils.OkHttpDownloadJsonUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

/**
 * Created by YJ on 2019/1/8.
 */
public class MyOrderActivity extends SwipeBackActivity {
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.srl)
    SmartRefreshLayout srl;
    @BindView(R.id.no_data)
    ImageView noData;
    @BindView(R.id.fukuannum)
    TextView fukuannum;
    @BindView(R.id.yuji)
    TextView yuji;
    @BindView(R.id.yongjin)
    TextView yongjin;
    @BindView(R.id.tablayout)
    TabLayout tablayout;
    @BindView(R.id.recycler2)
    RecyclerView recycler2;
    @BindView(R.id.srl2)
    SmartRefreshLayout srl2;
    private MyOrderAdapter myOrderAdapter;
    private MyOrderAdapter myOrder2Adapter;
    private int page = 1;
    private int page2 = 1;
    private int type = 0;
    private int teamtype = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_my);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .fitsSystemWindows(true)  //使用该属性,必须指定状态栏颜色
                .statusBarColor(R.color.white)
                .statusBarDarkFont(true, 0.2f)
                .init();
        type = getIntent().getIntExtra("type", 0);
        initData();
        setListener();
    }

    private void initData() {
        recycler.setLayoutManager(new LinearLayoutManager(MyOrderActivity.this));
        recycler.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        myOrderAdapter = new MyOrderAdapter(MyOrderActivity.this);
        recycler.setAdapter(myOrderAdapter);
        recycler2.setLayoutManager(new LinearLayoutManager(MyOrderActivity.this));
        recycler2.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        myOrder2Adapter = new MyOrderAdapter(MyOrderActivity.this);
        recycler2.setAdapter(myOrder2Adapter);
        getData();
        if (type == 0) {
            topTitle.setText("我的订单");
        } else {
            topTitle.setText("团队订单");
            tablayout.setVisibility(View.VISIBLE);
            tablayout.addTab(tablayout.newTab().setText("直属会员"), 0, true);
            tablayout.addTab(tablayout.newTab().setText("非直属会员"), 1, false);
            tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    switch (tab.getPosition()) {
                        case 0:
                            teamtype = 1;
                            getData();
                            break;
                        case 1:
                            teamtype = 2;
                            getData();
                            break;
                        default:
                    }
                }

                @Override
                public void onTabUnselected(TabLayout.Tab tab) {
                }

                @Override
                public void onTabReselected(TabLayout.Tab tab) {
                }
            });
        }
    }

    private void setListener() {
        srl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                srl.finishRefresh(2000);
                if (teamtype == 1) {
                    page = 1;
                } else {
                    page2 = 1;
                }
                getData();
            }
        });
        srl.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                srl.finishLoadMore(2000);
                if (teamtype == 1) {
                    page++;
                } else {
                    page2++;
                }
                getData();
            }
        });
    }

    @OnClick(R.id.top_back)
    public void onViewClicked() {
        finish();
    }

    private void getData() {
        if (type == 0) {
            OkHttpDownloadJsonUtil.downloadJson(this, Path.myOrder(UserBean.uid, page), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
                @Override
                public void onsendJson(String json) {
                    try {
                        Gson gson = new Gson();
                        MyOrder myOrder = gson.fromJson(json, MyOrder.class);
                        fukuannum.setText(myOrder.getTongji().getOrdernum() + "");
                        yuji.setText(myOrder.getTongji().getYugu());
                        yongjin.setText(myOrder.getTongji().getJiesuan());
                        if (page == 1 && myOrder.getData().size() == 0) {
                            srl.setVisibility(View.GONE);
                            noData.setVisibility(View.VISIBLE);
                            return;
                        }
                        myOrderAdapter.updateData(page, myOrder.getData());
                    } catch (Exception e) {
                        e.printStackTrace();
                        Toast.makeText(MyOrderActivity.this, "数据异常", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        } else {
            String url = "";
            if (teamtype == 1) {
                srl.setVisibility(View.VISIBLE);
                srl2.setVisibility(View.GONE);
                url = Path.teamOrders(UserBean.uid, teamtype, page);
            } else {
                srl.setVisibility(View.GONE);
                srl2.setVisibility(View.VISIBLE);
                url = Path.teamOrders(UserBean.uid, teamtype, page2);
            }
            OkHttpDownloadJsonUtil.downloadJson(this, url, new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
                @SuppressLint("SetTextI18n")
                @Override
                public void onsendJson(String json) {
                    try {
                        Gson gson = new Gson();
                        TeamOrder myOrder = gson.fromJson(json, TeamOrder.class);
                        fukuannum.setText(myOrder.getTongji().getOrdernum() + "");
                        yuji.setText(myOrder.getTongji().getYugu() + "");
                        yongjin.setText(myOrder.getTongji().getJiesuan() + "");
                        if (teamtype == 1) {
                            myOrderAdapter.updateData(page, myOrder.getTeam());
                            if (page == 1 && myOrder.getTeam().size() == 0) {
                                srl.setVisibility(View.GONE);
                                noData.setVisibility(View.VISIBLE);
                            } else if (myOrder.getTeam().size() == 0) {
                                Toast.makeText(MyOrderActivity.this, "暂无更多数据", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            myOrder2Adapter.updateData(page2, myOrder.getTeam());
                            if (page2 == 1 && myOrder.getTeam().size() == 0) {
                                srl2.setVisibility(View.GONE);
                                noData.setVisibility(View.VISIBLE);
                            } else if (myOrder.getTeam().size() == 0) {
                                Toast.makeText(MyOrderActivity.this, "暂无更多数据", Toast.LENGTH_SHORT).show();
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        Toast.makeText(MyOrderActivity.this, "数据异常", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }

}
